﻿using JuCloudy.Framework.Interface;
using JuCloudy.Framework.Storge;
using System;
using System.Collections.Generic;
using System.Data.Services.Client;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace JuCloudy.App.Toursim.Root
{
    public partial class ROOTPAGE : UserControl ,JuCloudy.Framework.Interface.IModule
    {
        public ROOTPAGE()
        {
            InitializeComponent();

            this.Loaded += (obj, e) =>
            {
                this.OnLoad();
            };
        }

        private void OnLoad()
        {
            this.MainRoot.Children.Clear();
            this.MainRoot.Children.Add(new ctrls.Open());
        }

        private void btn_CreateNew_Click(object sender, RoutedEventArgs e)
        {
            this.MainRoot.Children.Clear();
            this.MainRoot.Children.Add(new ctrls.Create());
        }

        private void btn_Open_Click(object sender, RoutedEventArgs e)
        {
            this.MainRoot.Children.Clear();
            this.MainRoot.Children.Add(new ctrls.Create());
        }

        public void InitResource()
        {
            AppDataService.AppDataContext ctx = new AppDataService.AppDataContext(new Uri(String.Format("http://{0}/ApiService/AppDataService.svc", Framework.Storge.VariableHelper.WebServiceHost)));
            var qryContents = (from c in ctx.ToursimContent select c) as DataServiceQuery<AppDataService.AppToursimContent>;
            qryContents.BeginExecute(new AsyncCallback((r) =>
            {
                var result = qryContents.EndExecute(r);
                Session.Client["tContents"] = result.ToList();
            }), null);

            var qryProducts = (from c in ctx.ToursimProduct select c) as DataServiceQuery<AppDataService.AppToursimProduct>;
            qryProducts.BeginExecute(new AsyncCallback((r)=>{
                var result = qryProducts.EndExecute(r);
                Session.Client["tProducts"] = result.ToList();
            }),null);
        }

        private void btn_BackToDefault_Click(object sender, RoutedEventArgs e)
        {
            JuCloudy.Framework.Storge.VariableHelper.Structure.HideCustomRoot();
        }
    }
}
